﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MeTalker.Aide.data;

namespace MeTalker.Aide.Forms
{
    internal partial class frmTypeEdit : MeForm
    {
       ImageHelper img;
        string ss1 = "fixed.bmp";
        string html = "<html><body style='margin:0;background-color:#bfdbff;text-align:canter;'><img src='{0}' alt='{1}'/></body></html>";
        
        internal string MessageInfo { get; private set; }

        internal Node messageNode;

        private frmTypeEdit()
        {
            InitializeComponent();//background-color:#D8ECE9
            this.Text = "编辑分类短语";
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            panelBmp.BackColor = Color.White;
            this.DoubleBuffered = true;
            int count = 0;
            for (int j = 0; j < 9; j++)
            {
                for (int i = 0; i < 11; i++)
                {
                    string picFileName = @"emotions\" + count.ToString() + "fixed.bmp";//图片文件名路路径
                    PictureBox pic = new PictureBox();
                    pic.Tag = count.ToString()+".gif";
                    pic.Name = "PicBox" + i.ToString();
                    pic.Size = new System.Drawing.Size(24, 24);
                    pic.MouseLeave += new EventHandler(PictureBox_MouseLeave);
                    pic.MouseEnter += new EventHandler(PictureBox_MouseEnter);
                    pic.MouseClick += new MouseEventHandler(PictureBox_Click);
                    Point picLocatoin = new Point(5 + i * 30, j * 32 + 5);
                    pic.Location = picLocatoin;
                    //加载图片
                    pic.Image = new Bitmap(picFileName);
                    //窗体类加子控件添加该picBox控件
                    // this.Controls.Add(pic);
                    this.panelBmp.Controls.Add(pic);
                    count++;
                }
            }

            webBrowser1.DocumentText = "<html><body style='margin:0;background-color:#bfdbff;text-align:canter;'></body></html>";
            lblKeyTip.Text = "";
            lblTitle.Text = "";
            //int i = 0;
            //foreach (var temp in DataFactory.ImageSources)
            //{
            //    PictureBox pic = new PictureBox();
            //    pic.ImageLocation = @"emotions/" + temp.FixedFile;

            //    pic.Location = new Point(i * 24, 0);
            //    this.panelBmp.Controls.Add(pic);
            //    i++;
            //    if (i > 5)
            //        break;
            //}

            //this.Load += new EventHandler(frmTypeEdit_Load);
            this.kryptonGroupBox1.Text = "短语分类";
        }

        public frmTypeEdit(Node node):this() {
            messageNode = node;
        }

        private void PictureBox_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pic = sender as PictureBox;
            if (pic != null)
            {
                SetGifStr(pic.Tag.ToString());
                //SetGifStr(pic.ImageLocation.Replace(ss1, ".gif").Replace(DataFactory.IMG_PATH, ""));
                //SetGifStr(pic.ImageLocation.Replace(DataFactory.IMG_PATH, ""));
            }
        }

        private void PictureBox_MouseLeave(object sender, EventArgs e)
        {
            SetGif(null);
        }

        private void PictureBox_Click(object sender, EventArgs e)
        {
            PictureBox pic = sender as PictureBox;
            if (pic != null)
            {
                //SetShortCutGif(pic.ImageLocation.Replace(DataFactory.IMG_PATH, ""));
                SetShortCutGif(pic.Tag.ToString());
            }
        }

        public frmTypeEdit(string msg, Node node)
            : this()
        {
            this.messageNode = node;
            //txtKeyword.Text = keyWorld;
            //this.mainForm = mainForm;
            int leftIndex =msg.IndexOf(UtilHelper.FAST_LEFT_SYMBOL);
            int rightIndex =msg.IndexOf(UtilHelper.FAST_RIGHT_SYMBOL);
            if (leftIndex >= 0 && rightIndex > 0)
            {
                this.txtCode.Text = msg.Substring(leftIndex + 1, rightIndex - 1);
                this.txtMessage.Text = msg.Substring(rightIndex + 1, msg.Length - (rightIndex + 1));
            }
            else
            {
                this.txtMessage.Text = msg;
            }
        }

        void frmTypeEdit_Load(object sender, EventArgs e)
        {
        }

        void SetShortCutGif(string iamge)
        {
            var d = from temp in DataFactory.ImageSources where temp.OriginalFile.Equals(iamge) select temp;
            ImageSource ds = d.FirstOrDefault();
            if (ds != null)
            {
                txtMessage.Text += ds.ShortCut;

            }
        }

        void SetGif(ImageSource iamge) {
            if (iamge != null)
            {
               // webBrowser1.Navigate(AppDomain.CurrentDomain.BaseDirectory + "emotions/" + iamge.OriginalFile);
                webBrowser1.DocumentText = string.Format(html, AppDomain.CurrentDomain.BaseDirectory + "emotions/" + iamge.OriginalFile, iamge.Meaning);
                lblTitle.Text = iamge.Meaning;
            }
            else
            {
                lblTitle.Text = "";//;background-color:#c0c0c0
                webBrowser1.DocumentText = "<html><body style='margin:0;background-color:#bfdbff;text-align:canter;'></body></html>";
            }
        }

        void SetGifStr(string iamge)
        {
            try
            {
                var d = from temp in DataFactory.ImageSources where temp.OriginalFile.Equals(iamge) select temp;
                ImageSource ds = d.FirstOrDefault();
                if (ds != null)
                    SetGif(ds);
            }
            catch { }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            lblKeyTip.Text = "";
            if (!txtCode.Text.Equals("")) {
                if (!char.IsLetter(txtCode.Text[0])) {
                    lblKeyTip.Text = "快捷键的首字符必须为字母！";
                    return;
                }
            }

            string code = txtCode.Text.Equals("") ? "" : (UtilHelper.FAST_LEFT_SYMBOL + txtCode.Text + UtilHelper.FAST_RIGHT_SYMBOL);
            MessageInfo = code + txtMessage.Text;
            DialogResult = DialogResult.Yes;
            this.Close();
            //SetGif(DataFactory.ImageSources[i]);
            //i++;
            ////img = new ImageHelper(txtCode.Text,this.panelGif);
            //img.Play();
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.No;
            //img.Stop();
            this.Close();
        }

        private void btnkeyword_Click(object sender, EventArgs e)
        {
            new frmKeyword(messageNode).ShowDialog();
        }

        private void btnKeywordLearning_Click(object sender, EventArgs e)
        {
            var ds = from temp in DataFactory.ChatRecords.Records where temp.Text.Equals(txtMessage.Text) select temp;
           Record record= ds.FirstOrDefault();
           string word = "";
           if (record != null)
           {
               word = record.Words;
           }
           MeMessageBox.ShowInformation("短语:" + txtMessage.Text + "\n\n自学到的关键字为:" + word + "\n\n");
        }
    }
}
